package com.lds.erp.category.vo;

import java.util.*;
import java.util.stream.Collectors;

import com.dph.common.utils.service.Base;
import com.dph.common.utils.utils.CommonUtils;
import com.lds.erp.common.YNEnum;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author pengxi
 * @apiNote
 * @date 2018/10/15 下午4:22
 */
@ApiModel(description = "商品分类基本信息")
public class CategoryVo extends Base {
    private final static Logger log = LoggerFactory.getLogger(CategoryVo.class);
    private static final long serialVersionUID = 7433084332945456215L;
    @ApiModelProperty(name = "id", value = "id")
    private Integer id;

    @ApiModelProperty(name = "value", value = "类别id")
    private Integer value;

    @ApiModelProperty(name = "label", value = "类别名称")
    private String label;

    @ApiModelProperty(name = "catStatus", value = "类别状态Y启用N禁用")
    private YNEnum catStatus;

    @ApiModelProperty(name = "parentId", value = "父级id")
    private Integer parentId;

    @ApiModelProperty(name = "children", value = "子类")
    private List<CategoryVo> children;

    @ApiModelProperty(name = "catLevel", value = "分类等级")
    private Integer catLevel;

    @ApiModelProperty(name = "catIcon", value = "分类图标")
    private String catIcon;

    public Integer getOrders() {
        return orders;
    }

    public void setOrders(Integer orders) {
        this.orders = orders;
    }

    private Integer orders;


    public String getCatIcon() {
        return catIcon;
    }

    public void setCatIcon(String catIcon) {
        this.catIcon = catIcon;
    }

    public Integer getCatLevel() {
        return catLevel;
    }

    public void setCatLevel(Integer catLevel) {
        this.catLevel = catLevel;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public YNEnum getCatStatus() {
        return catStatus;
    }

    public void setCatStatus(YNEnum catStatus) {
        this.catStatus = catStatus;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public List<CategoryVo> getChildren() {
        return children;
    }

    public void setChildren(List<CategoryVo> children) {
        this.children = children;
    }


    public static void addTreeNode(CategoryVo module, Map<Integer, CategoryVo> maps,
                                   List<CategoryVo> list) {
        if (maps.containsKey(module.getId())) {
            if (CommonUtils.isNotEmpty(maps.get(module.getId()).getChildren())) {
                module.setChildren(maps.get(module.getId()).getChildren());
            }
            maps.get(module.getId()).setEntity(module);
        } else {
            maps.put(module.getId(), module);
        }
        if (CommonUtils.isEmpty(module.getParentId())) {
            //一级
            list.add(maps.get(module.getId()));
        } else {
            if (maps.containsKey(module.getParentId())) {
                maps.get(module.getParentId()).addChildren(module);
            } else {
                CategoryVo parent = new CategoryVo();
                parent.addChildren(module);
                maps.put(module.getParentId(), parent);
            }
        }
    }

    //TODO 后期扩展分类时需要修改这里
    public static List<CategoryVo> getFristLevel(List<CategoryVo> tCategoryVo) {
        List<CategoryVo> result = new ArrayList<>();
        for (CategoryVo temp : tCategoryVo) {
            if (temp.getParentId() == null || temp.getParentId()==0) {
                result.add(temp);
            }
            //	result.add(temp);
            //}
            //<!-- cat_parentId=1  表示目前只查冻品分类-->
//            if (temp.getId()==1) {
//                result.add(temp);
//            }else if (temp.getId().intValue()==459){
//                result.add(temp);
//            }
        }
        return result;
    }

    public static void orders(List<CategoryVo> list) {
        list.stream().sorted(Comparator.comparing(CategoryVo::getOrders)).collect(Collectors.toList());
        for (CategoryVo temp : list) {
            if (CommonUtils.isNotEmpty(temp.getChildren()) && temp.getChildren().size() > 1) {
                orders(temp.getChildren());
            }
        }
    }

    public void addChildren(CategoryVo module) {
        if (CommonUtils.isEmpty(children)) {
            children = new ArrayList<>();
        }
        this.children.add(module);
    }

    /**
     * 获取商品分类树形结构数据
     *
     * @param moduleList
     * @return
     */
    public static List<CategoryVo> toTreeList(List<CategoryVo> moduleList) {
        Map<Integer, CategoryVo> maps = new HashMap<>();
        List<CategoryVo> list = new ArrayList<>();
        for (CategoryVo module : moduleList) {
            addTreeNode(module, maps, list);
        }
        List<CategoryVo> result = getFristLevel(list);
        //  对树结构每一层进行递归排序
        orders(result);
        return result;
    }

    public void setEntity(CategoryVo module) {
        setId(module.getId());
        setValue(module.getValue());
        setLabel(module.getLabel());
        setCatStatus(module.getCatStatus());
        setOrders(module.getOrders());
    }

    /**
     * 从2级开始获取树形结构
     */
    public static List<CategoryVo> toTreeListFromLevelTwo(List<CategoryVo> moduleList) {
        Map<Integer, CategoryVo> maps = new HashMap<>();
        List<CategoryVo> list = new ArrayList<>();
        for (CategoryVo module : moduleList) {
            addTreeNodeFromLevelTwo(module, maps, list);
        }
        return fristLevel(list);
    }

    /**
     * 商戶端专用树形结构
     */
    public static List<CategoryVo> toTreeListForBussniess(List<CategoryVo> moduleList, List<String> catIds) {
        Map<Integer, CategoryVo> maps = new HashMap<>();
        List<CategoryVo> list = new ArrayList<>();
        for (CategoryVo module : moduleList) {
            addTreeNodeFromForBusiness(module, maps, list);
        }

        return fristLevel(list);
    }

    public static void addTreeNodeFromLevelTwo(CategoryVo module, Map<Integer, CategoryVo> maps,
                                               List<CategoryVo> list) {
        if (maps.containsKey(module.getId())) {
            if (CommonUtils.isNotEmpty(maps.get(module.getId()).getChildren())) {
                module.setChildren(maps.get(module.getId()).getChildren());
            }
            maps.get(module.getId()).setEntity(module);
        } else {
            maps.put(module.getId(), module);
        }
        if (module.getCatLevel().equals(2)) {
            list.add(maps.get(module.getId()));
        } else {
            if (maps.containsKey(module.getParentId())) {
                maps.get(module.getParentId()).addChildren(module);
            } else {
                CategoryVo parent = new CategoryVo();
                parent.addChildren(module);
                maps.put(module.getParentId(), parent);
            }
        }
    }

    public static void addTreeNodeFromForBusiness(CategoryVo module, Map<Integer, CategoryVo> maps,
                                                  List<CategoryVo> list) {
        if (maps.containsKey(module.getId())) {
            if (CommonUtils.isNotEmpty(maps.get(module.getId()).getChildren())) {
                module.setChildren(maps.get(module.getId()).getChildren());
            }
            maps.get(module.getId()).setEntity(module);
        } else {
            maps.put(module.getId(), module);
        }

        if (CommonUtils.isEmpty(module.getParentId())) {
            list.add(maps.get(module.getId()));
        } else {
            if (maps.containsKey(module.getParentId())) {
                maps.get(module.getParentId()).addChildren(module);
            } else {
                CategoryVo parent = new CategoryVo();
                parent.addChildren(module);
                maps.put(module.getParentId(), parent);
            }
        }
    }

    public static List<CategoryVo> fristLevel(List<CategoryVo> tCategoryVo) {
        List<CategoryVo> result = new ArrayList<>();
        for (CategoryVo temp : tCategoryVo) {
            if (CommonUtils.isEmpty(temp.getParentId())) {
                result.add(temp);
            }
        }
        return result;
    }


}
